package com.yubest.device.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class LinuxDevice extends Device {

    @Override
    protected String getCpuSerial() throws Exception {
        String serialNumber = "";
        String[] shell = {"/bin/bash", "-c", "dmidecode -t processor | grep 'ID' | awk -F ':' '{print $2}' | head -n 1"};
        Process process = Runtime.getRuntime().exec(shell);
        process.getOutputStream().close();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = reader.readLine().trim();
        if (isNotBlank(line)) {
            serialNumber = line;
        }
        reader.close();
        return serialNumber;
    }

    @Override
    protected String getMainBoardSerial() throws Exception {
        String serialNumber = "";
        String[] shell = {"/bin/bash", "-c", "dmidecode | grep 'Serial Number' | awk -F ':' '{print $2}' | head -n 1"};
        Process process = Runtime.getRuntime().exec(shell);
        process.getOutputStream().close();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = reader.readLine().trim();
        if (isNotBlank(line)) {
            serialNumber = line;
        }
        reader.close();
        return serialNumber;
    }

    private boolean isNotBlank(String str) {
        return null != str && !"".equals(str.trim());
    }
}
